/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GoodsBrandContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseGoodsBrand;
import com.xteam.ecsystem.service.admin.GoodsBrandService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 商品品牌控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class GoodsBrandContorller {

	@Autowired
	private GoodsBrandService goodsBrandService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getGoodsBrandList.do")
	public void getGoodsBrandList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bgbName = ReqHelper.getRequestString(request, "bgbName");
		String bgbState = ReqHelper.getRequestString(request, "bgbState");
		
		String strList = this.goodsBrandService.getGoodsBrandList(pageIndex, pageSize, bgbName, bgbState);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateGoodsBrand.do")
	public void saveOrUpdateObject(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "goodsBrandInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseGoodsBrand goodsBrand = (BaseGoodsBrand)JSONObject.toBean(obj, BaseGoodsBrand.class);
		
		if(StringUtil.isNotNullOrEmpty(goodsBrand.getBgbId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSBRAND_MGR, 
				"saveOrUpdateObject", lslType, "BaseGoodsBrand");
		
		this.goodsBrandService.saveOrUpdateObject(goodsBrand, goodsBrand.getBgbId());
	}
	
	@RequestMapping("/getGoodsBrandInfoById.do")
	public void getGoodsBrandInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bgbId = ReqHelper.getRequestString(request, "bgbId");		
		String strInfo = this.goodsBrandService.getGoodsBrandInfoById(bgbId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/updateGoodsBrandStateById.do")
	public void updateGoodsBrandStateByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgbId = ReqHelper.getRequestString(request, "bgbId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsBrandService.updateGoodsBrandStateById(bgbId);

		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSBRAND_MGR, 
				"updateGoodsBrandStateById", ConstantLocation.LOG_Edit_STATE, "BaseGoodsBrand");
	}
	
	@RequestMapping("/delGoodsBgbLogo.do")
	public void delGoodsBgbLogo(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgbId = ReqHelper.getRequestString(request, "bgbId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsBrandService.delGoodsBgbLogo(bgbId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSBRAND_MGR, 
				"delGoodsBgbLogo", ConstantLocation.LOG_DELETE_PIC, "BaseGoodsBrand");

	}
	
	@RequestMapping("/delGoodsBrandByIds.do")
	public void delGoodsBrandByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bgbIds = ReqHelper.getRequestString(request, "bgbIds");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.goodsBrandService.delGoodsBrandByIds(bgbIds);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_GOODSBRAND_MGR, 
				"delGoodsBrandByIds", ConstantLocation.LOG_DELETE, "BaseGoodsBrand");

	}
	
	@RequestMapping("/getBgiByBgb.do")
	public void getBgiByBgb(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bgbIds = ReqHelper.getRequestString(request, "bgbIds");

		boolean isUsed = this.goodsBrandService.getBgiByBgb(bgbIds);
		if (isUsed) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
}
